package com.bluesky.drcp.business.service;

import java.util.List;

import com.bluesky.drcp.business.User;
import com.bluesky.drcp.persistence.dao.UserDao;

public class UserService {
	public static final String AUTH_TYPE_LOCAL = "local";

	public static final String AUTH_TYPE_REMOTE = "remote";

	private UserDao userDao;

	private AuthenticationService remoteAuthorizationService;

	public boolean login(String userId, String password) {

		User o = userDao.get(userId);
		if (o == null)
			return false;

		if (o.getAuthType().equals(AUTH_TYPE_LOCAL)) {
			o = userDao.get(userId, password);
			return (o != null);
		} else if (o.getAuthType().equals(AUTH_TYPE_REMOTE)) {
			if (remoteAuthorizationService == null)
				throw new RuntimeException(
						"remote authorization service is not available.");

			return remoteAuthorizationService.login(userId, password);
		} else
			throw new RuntimeException("unknow auth type:" + o.getAuthType());

	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public AuthenticationService getRemoteAuthorizationService() {
		return remoteAuthorizationService;
	}

	public void setRemoteAuthorizationService(
			AuthenticationService remoteAuthorizationService) {
		this.remoteAuthorizationService = remoteAuthorizationService;
	}
	
	public List getAll(){	
		return userDao.getAll();
	}
	
	public User get(String id){
		return userDao.get(id);
	}
	
	public void save(User user){
		userDao.save(user);
	}
	
	public void update(User user){
		userDao.update(user);
	}
	
	public void delete(String userId){
		userDao.delete(userId);
	}
}
